<template>
	<el-dialog :visible.sync="visible" :title="title">
		<el-form label-width="120px">
			<template v-if="shippingGoodsType == 'send'">
				<el-form-item label="收货人">
					<p>
						联系人: {{ message.consignee }}/{{ message.phone }}
						<fortress-copy :copy-data="message.consignee + '/' + message.phone"></fortress-copy>
					</p>
					<p>
						地址: {{ message.address }}
						<fortress-copy :copy-data="message.address"></fortress-copy>
					</p>
				</el-form-item>
				<!-- <el-form-item label="快递公司">
					<el-select v-model="formData.shipping" placeholder="请选择快递公司"></el-select>
				</el-form-item> -->
				<el-form-item label="快递单号">
					<el-input v-model="formData.expresssn" placeholder="请输入快递单号"></el-input>
				</el-form-item>
				<!-- <el-form-item label="计费方式">
					<el-radio-group v-model="formData.shippingFee">
						<el-radio :label="1">按订单发货</el-radio>
						<el-radio :label="2">商品分包裹发货</el-radio>
					</el-radio-group>
				</el-form-item> -->
			</template>
			<shipping-goods v-if="formData.shippingFee == 2" :type="shippingGoodsType"></shipping-goods>
			<template v-if="shippingGoodsType == 'cancel'">
				<el-input type="textarea" :autosize="{ minRows: 6 }" placeholder="取消发货原因..."></el-input>
			</template>
		</el-form>

		<template #footer>
			<el-button type="primary" @click="onConfirm">确 定</el-button>
			<el-button @click="onCancel">取 消</el-button>
		</template>
	</el-dialog>
</template>

<script>
import ShippingGoods from "./components/shipping-goods.vue"
import request from "@/common/request"

export default {
	name: "shipping",
	props: {
		message: {},
	},
	data() {
		return {
			visible: false,
			formData: {
				shipping: "",
				expresssn: "",
				shippingFee: 1,
				goods: [],
			},
			shippingGoodsType: "send", // cancel: 取消订单, send: 发货
			shipping: [],
		}
	},
	computed: {
		title() {
			return this.shippingGoodsType == "send" ? "订单发货" : "取消发货"
		},
	},
	async mounted() {},
	methods: {
		show(type) {
			this.shippingGoodsType = type
			this.visible = true
		},

		async onConfirm() {
			if (this.shippingGoodsType == "send") {
				if (!this.formData.expresssn) {
					this.$message.error("请输入快递单号")
					return
				}
				try {
					const { code, msg } = await request("post", "/orders/goodsOrder/delivery", {
						id: this.message.id,
						expresssn: this.formData.expresssn,
					})
					if (code == 0) {
						this.$message.success(msg)
						this.$emit("onConfirm")
					} else {
						this.$message.error(msg)
					}
				} catch (error) {}
			} else {
				try {
					const { code, msg } = await request("post", "/orders/goodsOrder/cancelDelivery", {
						id: this.message.id,
					})
					if (code == 0) {
						this.$message.success(msg)
						this.$emit("onConfirm")
					} else {
						this.$message.error(msg)
					}
				} catch (error) {}
			}
			this.onCancel()
		},
		onCancel() {
			this.visible = false

			this.formData.shipping = ""
			this.formData.expresssn = ""
			this.formData.shippingFee = 1
			this.formData.goods = []
		},
	},
	components: {
		ShippingGoods,
	},
}
</script>

<style lang="scss" scoped>
.shipping {
}
</style>
